package pages;

import core.TestBase;
import org.openqa.selenium.By;
import ui_test.TestData;
import utils.Log4Test;

import java.util.Set;

import static org.testng.Assert.assertTrue;

/**
 * Created with IntelliJ IDEA.
 * User: u
 * Date: 7/20/14
 * Time: 6:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class HomePage extends GeneralPage {
    protected By signInPanel = By.xpath("//*[@id='user_menu']//a[@name='signin']");
    protected By signInFaceBook = By.xpath("//form//div[@type='facebook']/a");
    protected By emailField = By.xpath("//*[@id='email']");
    protected By passField = By.xpath("//*[@id='pass']");
    protected By submitButton = By.xpath("//*[@type='submit']");
    protected By userNameTitle = By.xpath("//a[@class='m-user-link']");
    

    public void open() {
        webDriver.get(TestData.HomeURL);
        webDriver.manage().deleteAllCookies();
        webDriver.get("http://facebook.com");
        webDriver.manage().deleteAllCookies();
        webDriver.get(TestData.HomeURL);
        Log4Test.info("Open WebUrl " + TestData.HomeURL);
    }

    public boolean isOpened(String URL) {
        return webDriver.getCurrentUrl().equals(URL);
    }

    public void signInPaneClick() {
        try {
            Thread.sleep(1*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        webDriver.findElement(signInPanel).click();
    }

    public void signInViaFacebook() {

        Log4Test.info("Signing via Facebook, sending email and pass, submit clicking, to parent window back");
        Set<String> windowsBeforeClick = webDriver.getWindowHandles();
        webDriver.findElement(signInFaceBook).click();
        Set<String> windowsAfterClick = webDriver.getWindowHandles();
        windowsAfterClick.removeAll(windowsBeforeClick);
        webDriver.switchTo().window(windowsAfterClick.iterator().next());
        webDriver.findElement(emailField).sendKeys(TestData.email);
         
        webDriver.findElement(passField).sendKeys(getPass("pass"));
        webDriver.findElement(submitButton).click();
        webDriver.switchTo().window(windowsBeforeClick.iterator().next());
    }

    public void verifyUserNamePresent() {
        Log4Test.info("Verify userName appears at the homepage right corner");
        try {
            Thread.sleep(5*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        assertTrue(webDriver.findElement(userNameTitle).getText().equals(TestData.userName));
    }   
    public UserSettingsPage toUserPageChange(){
       webDriver.findElement(userNameTitle).click();
        return new UserSettingsPage();
    }
}
                              